using Microsoft.AspNetCore.Mvc;
using ProduceBasicData.Api.Services;
using ProduceBasicData.ErrorCode;
using Microsoft.AspNetCore.SignalR;
using ProduceBasicData.Api.Hubs;
using ProduceBasicData.Api.DTO;

namespace ProduceBasicData.Api.Controllers
{
    /// <summary>
    /// 通知测试控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class NotificationTestController : ControllerBase
    {
        private readonly INotificationService _notificationService;
        private readonly IHubContext<NotificationHub> _hubContext;
        private readonly ILogger<NotificationTestController> _logger;

        public NotificationTestController(
            INotificationService notificationService,
            IHubContext<NotificationHub> hubContext,
            ILogger<NotificationTestController> logger)
        {
            _notificationService = notificationService;
            _hubContext = hubContext;
            _logger = logger;
        }

        /// <summary>
        /// 测试发送报工单提交通知
        /// </summary>
        /// <param name="reportCode">报工单编号</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<bool>> TestProductionReportNotification(string reportCode = "BG00001")
        {
            try
            {
                var message = $"测试报工单{reportCode}已提交，请及时处理";
                var result = await _notificationService.SendProductionReportSubmitNotificationAsync(
                    reportCode, 
                    message
                );

                _logger.LogInformation($"测试报工单通知发送结果: {result.Code}, {result.Msg}");
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "测试报工单通知发送失败");
                return new ApiResult<bool>
                {
                    Code = ApiEnum.Failed,
                    Msg = $"测试失败: {ex.Message}",
                    Data = false
                };
            }
        }

        /// <summary>
        /// 测试直接发送SignalR消息
        /// </summary>
        /// <param name="message">测试消息</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<bool>> TestDirectSignalR(string message = "这是一条测试消息")
        {
            try
            {
                var notification = new RealtimeNotificationDto
                {
                    Title = "直接测试通知",
                    Content = message,
                    Type = 3, // 系统通知
                    TypeName = "系统通知",
                    Priority = 1, // 低优先级
                    PriorityColor = "#909399", // 灰色
                    CreateTime = DateTime.Now,
                    ShowAsPopup = true,
                    PopupDuration = 5000
                };

                await _hubContext.Clients.All.SendAsync("ReceiveNotification", notification);

                _logger.LogInformation($"直接SignalR消息发送成功: {message}");
                return new ApiResult<bool>
                {
                    Code = ApiEnum.Success,
                    Msg = "直接SignalR消息发送成功",
                    Data = true
                };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "直接SignalR消息发送失败");
                return new ApiResult<bool>
                {
                    Code = ApiEnum.Failed,
                    Msg = $"直接测试失败: {ex.Message}",
                    Data = false
                };
            }
        }

        /// <summary>
        /// 获取SignalR连接状态
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<object> GetSignalRStatus()
        {
            try
            {
                var status = new
                {
                    HubConfigured = _hubContext != null,
                    NotificationServiceConfigured = _notificationService != null,
                    Timestamp = DateTime.Now,
                    Message = "SignalR服务已配置"
                };

                return new ApiResult<object>
                {
                    Code = ApiEnum.Success,
                    Msg = "SignalR状态正常",
                    Data = status
                };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取SignalR状态失败");
                return new ApiResult<object>
                {
                    Code = ApiEnum.Failed,
                    Msg = $"获取状态失败: {ex.Message}",
                    Data = null
                };
            }
        }
    }
}



